package com.crm.miaohe.mapper;

import com.crm.miaohe.entity.order.OrderDetailPriceStrategyEntity;
import com.crm.miaohe.model.TblOrderDetails;
import com.crm.miaohe.model.TblOrderDetailsExample;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TblOrderDetailsMapper {
    long countByExample(TblOrderDetailsExample example);

    int deleteByExample(TblOrderDetailsExample example);

    @Delete({
        "delete from tbl_order_details",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into tbl_order_details (order_id, material_name, ",
        "material_code, spec_code, ",
        "spec_name, price, ",
        "num, delivery_date, ",
        "status, create_time, ",
        "create_user, update_time, ",
        "update_user, version,outstock_qty)",
        "values (#{orderId,jdbcType=BIGINT}, #{materialName,jdbcType=VARCHAR}, ",
        "#{materialCode,jdbcType=VARCHAR}, #{specCode,jdbcType=VARCHAR}, ",
        "#{specName,jdbcType=VARCHAR}, #{price,jdbcType=DOUBLE}, ",
        "#{num,jdbcType=INTEGER}, #{deliveryDate,jdbcType=TIMESTAMP}, ",
        "#{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, ",
        "#{createUser,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{updateUser,jdbcType=VARCHAR}, #{version,jdbcType=BIGINT}, #{outstockQty,jdbcType=DOUBLE})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(TblOrderDetails record);

    int insertSelective(TblOrderDetails record);

    List<TblOrderDetails> selectByExample(TblOrderDetailsExample example);

    List<OrderDetailPriceStrategyEntity> selectPriceStrategyByExample(@Param("orderId") Long orderId);

    @Select({
        "select",
        "tod.id as tod_id, tod.order_id as tod_order_id, tod.material_name as tod_material_name, ",
        "tod.material_code as tod_material_code, tod.spec_code as tod_spec_code, tod.spec_name as tod_spec_name, ",
        "tod.price as tod_price, tod.num as tod_num, tod.delivery_date as tod_delivery_date, ",
        "tod.status as tod_status, tod.create_time as tod_create_time, tod.create_user as tod_create_user, ",
        "tod.update_time as tod_update_time, tod.update_user as tod_update_user, tod.version as tod_version,",
        "tod.outstock_qty as tod_outstock_qty",
        "from tbl_order_details tod",
        "where tod.id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("com.crm.miaohe.mapper.TblOrderDetailsMapper.BaseResultMap")
    TblOrderDetails selectByPrimaryKey(Long id);

    int updateByExampleSelective(@Param("record") TblOrderDetails record, @Param("example") TblOrderDetailsExample example);

    int updateByExample(@Param("record") TblOrderDetails record, @Param("example") TblOrderDetailsExample example);

    int updateByPrimaryKeySelective(TblOrderDetails record);

    @Update({
        "update tbl_order_details",
        "set order_id = #{orderId,jdbcType=BIGINT},",
          "material_name = #{materialName,jdbcType=VARCHAR},",
          "material_code = #{materialCode,jdbcType=VARCHAR},",
          "spec_code = #{specCode,jdbcType=VARCHAR},",
          "spec_name = #{specName,jdbcType=VARCHAR},",
          "price = #{price,jdbcType=DOUBLE},",
          "num = #{num,jdbcType=INTEGER},",
          "delivery_date = #{deliveryDate,jdbcType=TIMESTAMP},",
          "status = #{status,jdbcType=INTEGER},",
          "create_time = #{createTime,jdbcType=TIMESTAMP},",
          "create_user = #{createUser,jdbcType=VARCHAR},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "update_user = #{updateUser,jdbcType=VARCHAR},",
          "version = #{version,jdbcType=BIGINT},",
          "outstock_qty = #{outstockQty,jdbcType=DOUBLE}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(TblOrderDetails record);

    int updateByMaterialIds(List<Long> materialIds);
}